#load package for endogenous market graph between schools
pacman::p_load(igraph)

#load data
# setwd(wd_data_final)
data_regression<-data_student_raw

#clean data
data_regression<-data_regression %>% group_by(an,scoala_de_provenienta) %>% mutate(ms_drop_perc=sum(is.na(id_bac))/n())
data_regression<-data_regression %>% mutate(drop=ifelse(is.na(id_bac),1,0))

data_regression<-data_regression %>% 
  dplyr::select(judet_bac,judet_adm,judet_ms,id_bac,media_la_admitere,id_adm,drop,ms_drop_perc,dec_town,entrance_perc,n_hs_town_group,dec_town,n_students_town_yr,n_hs_town,
                town,an,grad_perc,class_mean,school_mean,school_change,scoala_de_provenienta,unitate_de_invatamant,liceu_repartizat,
                school_harmonized,specializare_bac2,specializare_adm,Unemployment_hs_bac,Wages_hs_bac,drop_hs_hs_bac,
                town_hs_bac,Cod_SIRUTA_hs_bac,drop_middle_ms_adm,drop_hs_ms_adm)

data_regression<-as.data.frame(data_regression)
data_regression$n_hs_town_group<-data_regression$n_hs_town
data_regression$n_hs_town_group[data_regression$n_hs_town>=4 & data_regression$n_hs_town<=15]<-"4-15"
data_regression$n_hs_town_group[data_regression$n_hs_town>15]<-"16+"
data_regression$n_hs_town_group<-with(data_regression, reorder(n_hs_town_group, n_hs_town))

data_regression<-data_regression %>% group_by(Cod_SIRUTA_hs_bac) %>% mutate(n_school=length(unique(school_harmonized))) %>% ungroup
data_regression<-as.data.frame(data_regression)
data_regression<-data_regression %>% mutate(fe=paste0(town,":",scoala_de_provenienta))
data_regression<-data_regression %>% filter(an<=2019)
data_regression_orig<-data_regression

#knit IV other specifications for appendix (different controls)
#track
current_path<-rstudioapi::getActiveDocumentContext()$path
setwd(dirname(current_path))
rmarkdown::render("Endogenous_Markets.Rmd",knit_root_dir = getwd())

